/**
 * 
 */
package com.gkwave.module.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.gkwave.module.bean.BranchBean;
import com.gkwave.module.bean.FoodMenuBean;

/**
 * @author Administrator
 *
 */
public class FoodMenuDAO extends BaseDAO{

	private Statement stmt = null;
	private ResultSet rset = null;
	private Connection con = null;
	
	/**
	 * 
	 */
	public FoodMenuDAO() {
		// TODO Auto-generated constructor stub
	}

	public FoodMenuBean getLocationList(){
		String query = "SELECT t.id_location,t.nm_location FROM m_location t" ;
		System.out.println("Query: " + query);
		BaseDAO dao = null;
		FoodMenuBean xbean = null;
		try {
			dao = new BaseDAO();
			con = dao.openConnection();
			stmt = con.createStatement();
			rset = stmt.executeQuery(query);
			FoodMenuBean bean = null;
			
			if(!rset.wasNull()){
				System.out.println("result-set is not null");
				xbean = new FoodMenuBean();
			}
			while (rset.next()) {
				Integer idLocation = rset.getInt("t.id_location");
				String nmLocation = rset.getString("t.nm_location");
				bean = new FoodMenuBean(idLocation,nmLocation);
				xbean.entries.add(bean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dao.closeConnection(stmt, con , rset);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return xbean;
	}

	public FoodMenuBean getBranchesByLocation(Integer idLocation) {
		// TODO Auto-generated method stub
		String query = "SELECT t.id_branch,t.nm_branch FROM m_branch t where t.id_location='" + idLocation +"'" ;
		System.out.println("Query: " + query);
		BaseDAO dao = null;
		FoodMenuBean xbean = null;
		try {
			dao = new BaseDAO();
			con = dao.openConnection();
			stmt = con.createStatement();
			rset = stmt.executeQuery(query);
			FoodMenuBean bean = null;
			
			if(!rset.wasNull()){
				System.out.println("result-set is not null");
				xbean = new FoodMenuBean();
			}
			while (rset.next()) {
				Integer idBranch = rset.getInt("t.id_branch");
				String nmBranch = rset.getString("t.nm_branch");
				bean = new FoodMenuBean(idBranch,nmBranch,false);
				xbean.entries.add(bean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dao.closeConnection(stmt, con , rset);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return xbean;
	}

	public FoodMenuBean getFoodMenuListByBranch(Integer idBranch) {
		// TODO Auto-generated method stub
		String query = "SELECT t2.nm_branch,t3.nm_food_menu,t3.txt_food_menu FROM food_menu_detail t1,m_branch t2,m_food_menu t3 where t1.id_branch=t2.id_branch and t1.id_food_menu=t3.id_food_menu and t1.id_branch='" + idBranch +"'" ;
		System.out.println("Query: " + query);
		BaseDAO dao = null;
		FoodMenuBean xbean = null;
		try {
			dao = new BaseDAO();
			con = dao.openConnection();
			stmt = con.createStatement();
			rset = stmt.executeQuery(query);
			FoodMenuBean bean = null;
			
			if(!rset.wasNull()){
				System.out.println("result-set is not null");
				xbean = new FoodMenuBean();
			}
			while (rset.next()) {
//				Integer idFoodMenuDetail = rset.getInt("t1.id_food_menu_detail");
				String nmBranch = rset.getString("t2.nm_branch");
				String nmFoodMenu = rset.getString("t3.nm_food_menu");
				String txtFoodMenu = rset.getString("t3.txt_food_menu");
				bean = new FoodMenuBean(nmBranch,nmFoodMenu, txtFoodMenu);
				xbean.entries.add(bean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dao.closeConnection(stmt, con , rset);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return xbean;
	}
	
	public boolean saveFoodMenu(String nmFoodMenu, String txtFoodMenu) {
		// TODO Auto-generated method stub
		 String query = "INSERT INTO m_food_menu (nm_food_menu,txt_food_menu) VALUES "
						+ "('"
						+ nmFoodMenu
						+ "','"
						+ txtFoodMenu+ "')";
		System.out.println(query);
		boolean returnValue = false;
		BaseDAO dao = new BaseDAO();
		try {
			con = dao.openConnection();
			stmt = con.createStatement();
			int row = stmt.executeUpdate(query);
			if (row != 0) {
				returnValue = true;
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			returnValue = false;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnValue = false;
		} finally {
			try {
				dao.closeConnection(stmt, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return returnValue;
	}

	public boolean linkFoodMenu(String strBranch, String strFood) {
		boolean flag = false;
		String query = "INSERT INTO food_menu_detail (id_food_menu,id_branch) VALUES "
				+ "("
				+ Integer.parseInt(strFood)
				+ ","
				+ Integer.parseInt(strBranch) + ")";
		System.out.println(query);
		BaseDAO dao = new BaseDAO();
		try {
			con = dao.openConnection();
			stmt = con.createStatement();
			int row = stmt.executeUpdate(query);
			if (row != 0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = false;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = false;
		} finally {
			try {
				dao.closeConnection(stmt, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
	public void updateNotification(String strCity,String strBranch,String[] strFoodMenus){
		boolean flag = false;
		String fmenus = "";
		for(int i=0;i<strFoodMenus.length;i++){
			fmenus+= " "+strFoodMenus[i];
		}
		String message = "New foodmenu updates available at "+strCity+" - BRANCH - "+strBranch+" - MENU_ITEM - "+fmenus;
		String query = "INSERT INTO n_food_menu_detail (id_food_menu_detail,txt_notification) VALUES (0,'"+message+"')";
		
		System.out.println(query);
		BaseDAO dao = new BaseDAO();
		try {
			con = dao.openConnection();
			stmt = con.createStatement();
			int row = stmt.executeUpdate(query);
			if (row != 0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = false;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = false;
		} finally {
			try {
				dao.closeConnection(stmt, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public List<FoodMenuBean> getFoodMenuList() {
		// TODO Auto-generated method stub
		String query = "SELECT id_food_menu,nm_food_menu,txt_food_menu FROM m_food_menu" ;
		System.out.println("Query: " + query);
		BaseDAO dao = null;
		List<FoodMenuBean> list = null;
		try {
			dao = new BaseDAO();
			con = dao.openConnection();
			stmt = con.createStatement();
			rset = stmt.executeQuery(query);
			FoodMenuBean bean = null;
			list = new ArrayList<FoodMenuBean>();
			while (rset.next()) {
				Integer idFoodMenu = rset.getInt("id_food_menu");
				String nmFoodMenu = rset.getString("nm_food_menu");
				String txtFoodMenu = rset.getString("txt_food_menu");
				bean = new FoodMenuBean(idFoodMenu,nmFoodMenu,txtFoodMenu);
				list.add(bean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dao.closeConnection(stmt, con , rset);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}	
}
